<template>
  <el-main>
    <!-- 搜索栏-->
    <el-form
      ref="searchFormRef"
      :model="listParam"
      label-width="80px"
      :inline="true"
      size="small"
    >
      <el-form-item>
        <el-input v-model="listParam.title" placeholder="请输入公告标题" />
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" @click="searchBtn">查询</el-button>
        <el-button icon="el-icon-close" style="color: #ff7670;" @click="resetBtn">重置</el-button>
        <el-button v-permission="['sys:sysNotice:add']" type="primary" icon="el-icon-plus" @click="addBtn">新增</el-button>
      </el-form-item>
    </el-form>
    <!--
      data: 表格的数据
     -->
    <el-table :height="tableHeight" :data="tableData" border stripe>
      <el-table-column
        prop="title"
        label="公告标题"
      />
      <el-table-column
        prop="noticeContent"
        label="公告内容"
      />
      <el-table-column
        prop="createTime"
        label="发布时间"
      />
      <el-table-column v-if="$checkPermission(['sys:sysNotice:edit', 'sys:sysNotice:delete'])" label="操作" align="center" width="180">
        <template slot-scope="scope">
          <el-button v-permission="['sys:sysNotice:edit']" type="primary" size="small" icon="el-icon-edit" @click="editBtn(scope.row)">编辑</el-button>
          <el-button v-permission="['sys:sysNotice:delete']" type="danger" size="small" icon="el-icon-delete" @click="deleteBtn(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page.sync="listParam.currentPage"
      :page-sizes="[10, 20, 40, 80, 100]"
      :page-size="listParam.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="listParam.total"
      background
      @size-change="sizeChange"
      @current-change="currentChange"
    />
    <sys-dialog
      :title="dialog.title"
      :visible.sync="dialog.visible"
      :width="dialog.width"
      :height="dialog.height"
      @onClose="onClose"
      @onConfirm="onConfirm"
    >
      <div slot="content">
        <el-form
          ref="addModel"
          :model="addModel"
          :rules="rules"
          label-width="80px"
          size="small"
          style="margin-right: 40px;"
        >
          <el-form-item prop="title" label="公告标题">
            <el-input v-model="addModel.title" placeholder="" />
          </el-form-item>

          <el-form-item prop="noticeContent" label="公告内容">
            <el-input v-model="addModel.noticeContent" type="textarea" placeholder="" />
          </el-form-item>

        </el-form></div>
    </sys-dialog>

  </el-main>
</template>

<script>
// 引入弹窗组件
import { addNoticeApi, updateNoticeApi, deleteNoticeApi, listNoticeApi } from '@/api/notice'
import SysDialog from '@/components/dialog/SysDialog.vue'
export default {
  components: { SysDialog },
  data() {
    return {
      // 表单绑定的数据
      addModel: {
        title: '',
        type: '', // 0:新增，1:编辑
        noticeContent: '',
        noticeId: '' // 编辑时

      },
      rules: {
        roleId: [{
          title: true,
          required: true,
          message: '请输入公告标题',
          trigge: 'blur'
        }],
        noticeContent: [{
          required: true,
          message: '请输入公告内容',
          trigge: 'blur'
        }]
      },
      // 弹窗属性
      dialog: {
        title: '',
        visible: false,
        width: 630,
        height: 250
      },
      // 定义表格高度
      tableHeight: 0,
      listParam: {
        title: '',
        currentPage: 1,
        pageSize: 10,
        total: 0
      },
      // 表格数据
      tableData: [],
      // 角色列表数据
      roleList: []
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.tableHeight = window.innerHeight - 220
    })
  },
  created() {
    this.getList()
    this.getRoleList()
  },
  methods: {
    // 获取列表
    async getList() {
      const result = await listNoticeApi(this.listParam)
      if (result && result.code === 200) {
        this.tableData = result.data.records
        this.listParam.total = result.data.total
      }
    },
    // 弹窗关闭
    onClose() {
      this.dialog.visible = false
    },
    // 弹框确定
    onConfirm() {
      this.$refs.addModel.validate(async(valid) => {
        if (valid) {
          let res = null
          if (this.addModel.type === '0') {
            res = await addNoticeApi(this.addModel)
          } else {
            res = await updateNoticeApi(this.addModel)
          }
          if (res && res.code === 200) {
            // 信息提示
            this.$message({
              type: 'success',
              message: res.msg
            })
            // 刷新列表
            this.getList()
            this.dialog.visible = false
          }
        }
      })
    },
    // 页数改变是触发
    currentChange(val) {
      this.listParam.currentPage = val
      this.getList()
    },
    // 页容量改变时触发
    sizeChange(val) {
      this.listParam.pageSize = val
      this.getList()
    },
    // 编辑按钮
    async editBtn(row) {
      console.log(row)
      this.dialog.visible = true
      this.addModel.type = '1'
      this.dialog.title = '编辑用户'
      //  清空表格数据
      // this.$resetForm('addModel', this.addModel)
      if (this.$refs.addModel !== undefined) {
        this.$refs.addModel.resetFields()
      }
      // 把当前要编辑的数据复制到表单绑定的数据域
      this.$objCopy(row, this.addModel)
      this.addModel.noticeId = row.noticeId
    },
    // 删除按钮
    async deleteBtn(row) {
      // 信息确认
      const confirm = await this.$myconfirm('确定删除该数据吗？')
      if (confirm) {
        const res = await deleteNoticeApi({ noticeId: row.noticeId })
        if (res && res.code === 200) {
          // 信息提示
          this.$message({
            type: 'success',
            message: res.msg
          })
          // 刷新列表
          this.getList()
        }
      }
    },
    // 新增按钮
    addBtn() {
      this.dialog.title = '新增公告'
      if (this.$refs.addModel !== undefined) {
        this.$refs.addModel.resetFields()
        this.$emptyData(this.addModel)
      }
      this.addModel.type = '0'
      this.dialog.visible = true
    },
    // 查询按钮
    searchBtn() {
      this.getList()
    },
    // 重置按钮
    resetBtn() {
      this.listParam.title = ''
      this.getList()
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
